/**
 * Created by 佟大为 on 2018/7/10.
 */

$(function () {
    $("#dic_datagrid").datagrid({
        fit:true,
        url:"/dictionary/list",
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        striped:true,
        toolbar:"#tb",
        singleSelect:true,
        columns:[[
            {field:"ck",checkbox:true},  //显示多选框样式
            {field:"id", title:"编号", hidden:true},
            {field:"name", title:"数据名", width:100,formatter: function (value,row) {
                return "<p>"+row.name+"</p>"
            }},
            {field:"sn", title:"数据编号", width:100}
        ]],
        onClickRow: function(index, row) {
            $("#single_datagrid").datagrid({
                fit:true,
                url:"/dictionary/selectDictionarySingleBySn?sn="+row.sn,
                fitColumns:true,
                pagination:true,
                rownumbers:true,
                striped:true,
                toolbar:"#tb2",
                singleSelect:true,
                columns:[[
                    {field:"ck",checkbox:true},  //显示多选框样式
                    {field:"id", title:"编号", hidden:true},
                    {field:"name", title:"名称", width:100,formatter: function (value,row) {
                        return "<p>"+row.name+"</p>"
                    }},
                    {field:"introduce", title:"简介", width:100}
                ]]
            });

        }
    });



    $("#dic_dialog").dialog({
        width:400,
        height:500,
        buttons:"#bb",
        closed:true
    });
    $("#single_dialog").dialog({
        width:400,
        height:500,
        buttons:"#bb2",
        closed:true
    });

});


$("#dic_datagrid").datagrid("onClickRow",function (index, row) {
    $("#single_datagrid").datagrid({
        fit:true,
        url:"/dictionary/selectDictionarySingleBySn?sn="+row.sn,
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        striped:true,
        toolbar:"#tb2",
        singleSelect:true,
        columns:[[
            {field:"ck",checkbox:true},  //显示多选框样式
            {field:"id", title:"编号", hidden:true},
            {field:"name", title:"名称", width:100},
            {field:"introduce", title:"简介", width:100}
        ]]
    });


    $("#single_dialog").dialog({
        width:400,
        height:500,
        buttons:"#bb2",
        closed:true
    });
});






function edit(){
    var row = $("#dic_datagrid").datagrid("getSelected");
    if(!row){
        $.messager.alert('温馨提示',"请选择要编辑的数据");
        return;
    }
    $("#dic_form").form("clear");
    $("#dic_form").form("load", row);
    $("#dic_dialog").dialog("open");
    $("#dic_dialog").dialog("setTitle", "字典数据编辑");
}
function edit2(){
    var row = $("#single_datagrid").datagrid("getSelected");
    if(!row){
        $.messager.alert('温馨提示',"请选择要编辑的数据");
        return;
    }
    $("#single_form").form("clear");
    if(row.dictionary){
        row["dictionary.sn"] = row.dictionary.sn;
    }
    $("#single_form").form("load", row);
    $("#single_dialog").dialog("open");
    $("#single_dialog").dialog("setTitle", "编辑");
}

function add(){
    //添加与编辑时将数据清空
    $("#dic_form").form("clear");
    //1:弹框
    $("#dic_dialog").dialog("open");
    //2:设置标题
    $("#dic_dialog").dialog("setTitle", "字典数据添加");
}
function add2(){
    //添加与编辑时将数据清空
    $("#single_form").form("clear");
    //1:弹框
    $("#single_dialog").dialog("open");
    //2:设置标题
    $("#single_dialog").dialog("setTitle", "添加");
}

function save() {
    var id = $("#dicId").val();
    var url =  "/dictionary/save";
    if(id){
        url =  "/dictionary/update";
    }
    $("#dic_form").form('submit', {
        url: url,
        success: function (data) {
            data = $.parseJSON(data);

            if(!data.success){
                $.messager.alert('温馨提示',data.msg);

            }else{
                //关闭框
                $("#dic_dialog").dialog("close");
                //刷新列表数据
                $("#dic_datagrid").datagrid("load");
            }
        }
    })
}
function save2() {
    var id = $("#singleId").val();
    var url =  "/dictionarySingle/save";
    if(id){
        url =  "/dictionarySingle/update";
    }
    $("#single_form").form('submit', {
        url: url,
        success: function (data) {
            data = $.parseJSON(data);

            if(!data.success){
                $.messager.alert('温馨提示',data.msg);

            }else{
                //关闭框
                $("#single_dialog").dialog("close");
                //刷新列表数据
                $("#single_datagrid").datagrid("load");
            }
        }
    })
}

function cancel(){
    $("#dic_dialog").dialog("close");
}
function cancel2(){
    $("#single_dialog").dialog("close");
}

function remove(){
    //判断是否选中数据
    var row = $("#dic_datagrid").datagrid("getSelected");
    if(!row){
        $.messager.alert('温馨提示',"请选择要删除的数据");
        return;
    }

    $.messager.confirm('确认','您确认想要执行这个操作？',function(r){
        if (r){

            //1:发起请求修改状态
            $.get("/dictionary/delete",{id:row.id}, function(data){
                if(data.success){
                    //2:刷新列表数据
                    $("#dic_datagrid").datagrid("load");
                }else{
                    $.messager.alert('温馨提示',data.msg);
                }
            })
        }
    });
}function remove2(){
    //判断是否选中数据
    var row = $("#single_datagrid").datagrid("getSelected");
    if(!row){
        $.messager.alert('温馨提示',"请选择要删除的数据");
        return;
    }

    $.messager.confirm('确认','您确认想要执行这个操作？',function(r){
        if (r){

            //1:发起请求修改状态
            $.get("/dictionarySingle/delete",{id:row.id}, function(data){
                if(data.success){
                    //2:刷新列表数据
                    $("#single_datagrid").datagrid("load");
                }else{
                    $.messager.alert('温馨提示',data.msg);
                }
            })
        }
    });
}